package com.xsp.ucenter.controller;


import com.xsp.commonutils.JwtUtils;
import com.xsp.commonutils.R;
import com.xsp.ordervo.UcenterMemberOrder;
import com.xsp.ucenter.entity.Member;
import com.xsp.ucenter.entity.vo.LoginVo;
import com.xsp.ucenter.entity.vo.RegisterVo;
import com.xsp.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author xsp
 * @since 2022-02-21
 */
@RestController
@RequestMapping("/educenter/member")
public class MemberController {
    @Autowired
    MemberService memberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        String token = memberService.login(loginVo);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    //登录后获取信息
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        //传入request，拿到header头的token，解析后得到id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //通过id在数据库查出整个对象
        Member member = memberService.getLoginInfo(memberId);
        return R.ok().data("member",member);
    }

    //根据token字符串获取用户信息
    @PostMapping("getInfoUcenter/{id}")
    public UcenterMemberOrder getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        Member member = memberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        //把获取到的值给UcenterMemberOrder对象
        //实际上Member和UcenterMemberOrder的内容是相同的
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //统计某一天的注册人数
    @GetMapping("registerCount/{day}")
    public R registerCount(@PathVariable String day) {
        Integer countByDay = memberService.registerCountByDay(day);
        return R.ok().data("countByDay",countByDay);
    }

}

